from django.http import HttpResponse, HttpResponseRedirect, HttpRequest, Http404
from django.shortcuts import render_to_response
from posts.models import *
from slider.views import get_slider

def index(request):
    items = Post.objects.filter(type=POST_TYPE_NOTE).order_by('-cdate')
    slider = get_slider()
    return render_to_response('post_list.html', {'items':items, 'slider':slider})

def show(request, *args, **kwargs):
    all_items = Post.objects.filter(type = kwargs['post_type'])
    if kwargs.get('object_id'):
        try:
            item = all_items.get(pk = kwargs.get('object_id'))
        except Post.DoesNotExist:
            raise Http404
    elif kwargs.get('object_url'):
        try:
            item = all_items.get(url = kwargs.get('object_url'))
        except Post.DoesNotExist:
            raise Http404
    return render_to_response('post_show.html', {'item':item})

def list(request, *args, **kwargs):
    all_items = Post.objects.filter(type = kwargs.get('post_type'))
    items = all_items.order_by('-cdate')
    return render_to_response('post_list.html', {'items':items})

def cat_list(request, *args, **kwargs):
    all_items = Post.objects.all()

    if kwargs.get('cat_id'):
        try:
            cat = Category.objects.get(pk = kwargs.get('cat_id'))
        except Category.DoesNotExist:
            raise Http404
    elif kwargs.get('cat_url'):
        try:
            cat = Category.objects.get(url = kwargs.get('cat_url'))
        except Category.DoesNotExist:
            raise Http404
    items = cat.post_set.filter(status__in = FRONT_STATUS).order_by('-cdate')
    return render_to_response('cat_list.html', {'items':items})